/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ServiceController;

import com.Service.BO.DepartmentBO;
import com.Service.BO.SuperBO;
import com.Service.DAO.SuperDAO;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author olugbokikisemiu
 */
public class AddDepController extends HttpServlet {

    SuperDAO department;
    DepartmentBO getdep;

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();

        department = new SuperDAO();
        getdep = new DepartmentBO();

        try {

            String name = request.getParameter("depname");
            String desc = request.getParameter("description");
            String led = request.getParameter("leadcontact");
            String add1 = request.getParameter("addressline1");
            String add2 = request.getParameter("addressline2");
            String add3 = request.getParameter("addressline3");
            String post = request.getParameter("postcode");
            String cit = request.getParameter("city");
            String state = request.getParameter("county");
            int count = Integer.parseInt(request.getParameter("country"));
            String tpBiz = request.getParameter("type");
            String fulldesc = request.getParameter("full");
            String code = request.getParameter("code");
            String fone = request.getParameter("phone");
            String vax = request.getParameter("fax");
            String mail = request.getParameter("email");
            String web = request.getParameter("webAdd");
            int direct = Integer.parseInt(request.getParameter("diet"));
            String active = request.getParameter("Inactive");

            getdep.setName(name);
            getdep.setDesc(desc);
            getdep.setLead(led);
            getdep.setAddline1(add1);
            getdep.setAddline2(add2);
            getdep.setAddline3(add3);
            getdep.setPostcode(post);
            getdep.setCity(cit);
            getdep.setCounty(state);
            getdep.getCountry().setID(count);
            getdep.setTypeofBis(tpBiz);
            getdep.setFullDes(fulldesc);
            getdep.setSicCode(code);
            getdep.setPhone(fone);
            getdep.setFax(vax);
            getdep.setEmail(mail);
            getdep.setWebAdd(web);
            getdep.getDirectID().setId(direct);


            if (active != null) {
                getdep.setIsActive("True");
            } else {
                getdep.setIsActive("False");
            }

            department.DepartmentSave(getdep);
            RequestDispatcher req = request.getRequestDispatcher("DirectorateController");
            req.forward(request, response);
            //System.out.println("Dep name  " +getdep.getDirectID().getId());
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
